<?php
  require_once '../../Helpers/DbHelper.php';
  require_once '../../Controllers/AdminController.php';
  require_once '../../Controllers/UserController.php';
  require_once '../../Helpers/PathHelper.php';
  //
  $u = new UserController();
  $u->evaluatePagePermission(UserController::ADMINISTRATOR);
  //
  $c = new AdminController();
  $yearLevelList = $c->getYearLevel();
  $id = $_GET['id'];
  //
  if(!empty($_POST))
  {
    if(isset($_POST['show-fees']))
    {
      $yearLevelId = $_POST['yearLevel'];
      $feesByYearLevel = $c->getFeesByYearLevel($yearLevelId);
    }
    if(isset($_POST['save-fees']))
    {
      $status = $c->addStudentFees($id);
      //
      if($status)
        $message = 'Total Fees successfully added. Please proceed to the next step.';
      else
        $message = 'Total Fees not successfully added. Please try again.';
    }
  }
?>
<html>
  <head>
    <title>School Enrolment :: School Fees</title>
    <?php
      require_once '../commons.php';
      renderHeaderStyles() ;
    ?>
  </head>
  <body>
  <div class="admin-container">
    <?php renderAdminNav(); ?>
  </div>
  <div class="container">
    <h1>Input Fees</h1>
    <form name="fees-form" method="post">
      <label for="year-level">Choose a year level: </label>
      <select name="yearLevel">
        <?php
          if(mysql_num_rows($yearLevelList) < 1)
            echo "<option value='0'>No data available</option>";

          while($rows=mysql_fetch_assoc($yearLevelList))
          {
            $i = $rows['YearLevelId'];
            $n = $rows['YearLevelDescription'];

            $content = "<option value='$i'";
            if($i==$yearLevelId)
              $content.= "selected";
            $content .=">$n</option>";

            echo $content;
          }
        ?>
      </select><br /><br />
      <input type="submit" name="show-fees" value="Show Fees" />
    </form>
    <hr />
    <form name="fees-list" method="post">
    <table cellspacing="5" align="center" class="table">
      <tr>
        <th width="150">Fee Description</th>
        <th>Amount (PHP)</th>
      </tr>
      <?php
        if(mysql_num_rows($feesByYearLevel) < 1)
          echo "<tr><td colspan='3'>No data available.</td></tr>";
        else {
          $sum = 0;
          while($rows = mysql_fetch_assoc($feesByYearLevel))
          {
            //print_r($feesByYearLevel);
            $feesName = $rows['FeeName'];
            $feeAmount = $rows['FeeAmount'];
            $sum+=$feeAmount;

            echo "<tr>
              <td>$feesName</td>
              <td>$feeAmount</td>
            </tr>";
          }
            echo "<tr><td>Total: $sum</td></tr>";
            echo '<tr><td><input type="submit" name="save-fees" value="Save Fees" /></td></tr>
            <tr><td><input type="hidden" name="total" value="'.$sum.'" /></td></tr>';

        }
      ?>
    </table>
    <div class="notices"><?php echo $message?></div>
    </form>
    <a id="prev-next" href="<?php echo PathHelper::getBaseUrl().'/Views/Admin/invoice.php?id='.$id?>">Next Step &rarr;</a>

  </div>
  </body>
</html>